/**
 * @Title: ConfigController.java
 * @Package me.springboot.mybatis.web
 * @Description: TODO
 * @author zengh
 * @date 2018/04/24
 * @version V1.0
 */
package me.springboot.mybatis.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.springboot.mybatis.common.utils.RoleType;
import me.springboot.mybatis.common.utils.SessionUtil;
import me.springboot.mybatis.core.RetCode;
import me.springboot.mybatis.core.RetResponse;
import me.springboot.mybatis.core.RetResult;
import me.springboot.mybatis.exception.ResultException;
import me.springboot.mybatis.model.Config;
import me.springboot.mybatis.service.ConfigService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zengh
 * @ClassName: ConfigController
 * @Description: TODO
 * @date 2018/04/24
 */
@RestController
@RequestMapping("/config")
@CrossOrigin
public class ConfigController {

    @Resource
    private ConfigService configService;

    /**
     * @param config
     * @Title: add
     * @Description: 添加
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/add")
    public RetResult<Object> add(HttpServletRequest request, Config config) throws ResultException {
        if(!RoleType.isKehu(request)) {
            throw new ResultException(RetCode.NOT_RIGHT.getCode(), "没有权限");
        }
        config.setKehuUserid(SessionUtil.getSessionUser(request).getuId());
        configService.save(config);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param config
     * @Title: add2
     * @Description: 传入参数为JSON格式
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/add2")
    public RetResult<Object> add2(HttpServletRequest request, @RequestBody Config config) throws ResultException {
        if(!RoleType.isKehu(request)) {
            throw new ResultException(RetCode.NOT_RIGHT.getCode(), "没有权限");
        }
        config.setKehuUserid(SessionUtil.getSessionUser(request).getuId());
        configService.save(config);
        return RetResponse.makeOKRsp();
    }

    /**
     * @Title: delete
     * @Description: 删除
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/delete")
    public RetResult<Object> delete(@RequestParam Integer id) {
        configService.deleteById(id);
        return RetResponse.makeOKRsp();
    }

    /**
     * @Title: update
     * @Description: 更新
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/update")
    public RetResult<Object> update(HttpServletRequest request, Config config) throws ResultException {
        if(!RoleType.isKehu(request)) {
            throw new ResultException(RetCode.NOT_RIGHT.getCode(), "没有权限");
        }
        config.setKehuUserid(SessionUtil.getSessionUser(request).getuId());
        configService.updateConfig(config);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param config
     * @Title: update
     * @Description: 更新(传入参数为JSON格式)
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/update2")
    public RetResult<Object> update2(@RequestBody Config config) {
        configService.update(config);
        return RetResponse.makeOKRsp();
    }

    /**
     * @Title: detail
     * @Description: 根据ID查询详情
     * @Reutrn RetResult<Config>
     */
    @PostMapping("/detail")
    public RetResult<Config> detail(@RequestParam Integer id) {
        Config config = configService.findById(id);
        return RetResponse.makeOKRsp(config);
    }

    /**
     * @param reqMap
     * @Title: detail
     * @Description: 传入参数为JSON格式
     * @Reutrn RetResult<Config>
     */
    @PostMapping("/detail2")
    public RetResult<Config> detail(@RequestBody Map<String, Object> reqMap) {
        Integer id = Integer.valueOf(reqMap.get("id").toString());
        Config config = configService.findById(id);
        return RetResponse.makeOKRsp(config);
    }

    /**
     * @param page 页码
     * @param size 每页条数
     * @Title: list
     * @Description: 分页查询
     * @Reutrn RetResult<PageInfo<Config>>
     */
    @PostMapping("/list")
    public RetResult<PageInfo<Config>> list(HttpServletRequest request,@RequestParam(defaultValue = "0") Integer page,
                                            @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Config> list = configService.findAll();
        PageInfo<Config> pageInfo = new PageInfo<Config>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }

    @PostMapping("/getAll")
    public RetResult<List<Config>> getAll(HttpServletRequest request, String devicetype) throws ResultException {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("devicetype", devicetype);
        if(RoleType.isKehu(request)) {
            paramMap.put("kehu_userid", SessionUtil.getSessionUser(request).getuId() + "");
        }

        List<Config> list = configService.selectByMap(paramMap);
        return RetResponse.makeOKRsp(list);
    }
}